Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

@AliAlimohammadi AliAlimohammadi commented Jan 30, 2026

Description

This PR adds an implementation of the Jarvis March algorithm (also known as the Gift Wrapping algorithm) for computing the convex hull of a set of 2D points.

The convex hull is the smallest convex polygon that contains all given points. The Jarvis March algorithm is an output-sensitive algorithm with time complexity $O(n·h)$, where $n$ is the number of input points and $h$ is the number of points on the convex hull.

What's Included

  • jarvis_march.py: Complete implementation in the geometry directory
  • Point class: Represents 2D coordinates
  • Helper functions: Cross product calculation and point-on-segment detection
  • Comprehensive doctests: 33 tests covering various scenarios including:
    • Simple shapes (triangles, rectangles)
    • Complex shapes (star patterns)
    • Edge cases (collinear points, duplicate points, too few points)
    • Interior points exclusion

Algorithm Details

  • Time Complexity: $O(n·h)$ where $n$ is the number of points and $h$ is the hull size
  • Space Complexity: $O(h)$ for storing hull points
  • Approach:
    1. Find the leftmost point as the starting point
    2. Iteratively select the most counter-clockwise point from the current point
    3. Handle collinear points to avoid adding unnecessary vertices
    4. Return empty list for degenerate cases (< 3 non-collinear points)

Testing

All doctests pass successfully:

$ python jarvis_march.py -v
33 tests in 9 items.
33 passed and 0 failed.
Test passed.

References

Checklist

  • Code follows the repository's style guidelines
  • Comprehensive docstrings with examples
  • All doctests pass
  • Type hints included
  • Algorithm has proper documentation with complexity analysis
  • No duplicate of existing algorithms in the repository

@algorithms-keeper algorithms-keeper bot added the require descriptive names This PR needs descriptive function and/or variable names label Jan 30, 2026
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Jan 30, 2026
@algorithms-keeper algorithms-keeper bot removed the require descriptive names This PR needs descriptive function and/or variable names label Jan 30, 2026
@AliAlimohammadi
Copy link
Contributor Author

@poyea, this is ready to be merged.

@AliAlimohammadi AliAlimohammadi requested a review from poyea February 2, 2026 05:54
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Feb 2, 2026
@algorithms-keeper algorithms-keeper bot added the require tests Tests [doctest/unittest/pytest] are required label Feb 2, 2026
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

self.x = x_coordinate
self.y = y_coordinate

def __eq__(self, other: object) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file geometry/jarvis_march.py, please provide doctest for the function __eq__

return NotImplemented
return self.x == other.x and self.y == other.y

def __repr__(self) -> str:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file geometry/jarvis_march.py, please provide doctest for the function __repr__

def __repr__(self) -> str:
return f"Point({self.x}, {self.y})"

def __hash__(self) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file geometry/jarvis_march.py, please provide doctest for the function __hash__

return hash((self.x, self.y))


def _cross_product(origin: Point, point_a: Point, point_b: Point) -> float:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file geometry/jarvis_march.py, please provide doctest for the function _cross_product

)


def _is_point_on_segment(p1: Point, p2: Point, point: Point) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file geometry/jarvis_march.py, please provide doctest for the function _is_point_on_segment

) <= point.y <= max(p1.y, p2.y)


def jarvis_march(points: list[Point]) -> list[Point]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file geometry/jarvis_march.py, please provide doctest for the function jarvis_march

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require tests Tests [doctest/unittest/pytest] are required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants